package com.java2007.xiazhaodong.hotel.service.impl;


import com.java2007.xiazhaodong.hotel.dao.FoodTypeDao;
import com.java2007.xiazhaodong.hotel.entity.FoodType;
import com.java2007.xiazhaodong.hotel.factory.BeanFactory;
import com.java2007.xiazhaodong.hotel.service.FoodTypeService;
import com.java2007.xiazhaodong.hotel.utils.DbUtils;

import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2021/1/19 20:20
 * @Version 1.0
 */
public class FoodTypeServiceImpl implements FoodTypeService {
    //原先直接创建对象，写死了
    //private FoodTypeDao foodTypeDao=new FoodTypeDaoImpl();

    //现在改由工厂创建对象
    private FoodTypeDao foodTypeDao= BeanFactory.getInstance("foodtypeDao",FoodTypeDao.class);

    //添加
    @Override
    public void save(FoodType foodType) {
        try {
            DbUtils.begin();
            foodTypeDao.save(foodType);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            throw new RuntimeException(e);
        }

    }
    //更新
    @Override
    public void update(FoodType foodType) {
        try {
            DbUtils.begin();
            foodTypeDao.update(foodType);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            throw new RuntimeException(e);
        }
    }
    //删除
    @Override
    public void delete(int id) {
        try {
            DbUtils.begin();
            foodTypeDao.delete(id);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            throw new RuntimeException(e);
        }
    }
    //根据id查询
    @Override
    public FoodType findById(int id) {
        try {
            return foodTypeDao.findById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //查询全部
    @Override
    public List<FoodType> getAll() {
        try {
            return foodTypeDao.getAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //根据菜系名模糊查询所有
    @Override
    public List<FoodType> getAll(String typeName) {
        try {
            return foodTypeDao.getAll(typeName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
